package com.yinda.dt.produce.module.main;


import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.MarkerView;
import com.yinda.dt.produce.R;
import com.yinda.dt.produce.utils.LineChartUtil;
import com.yinda.dt.produce.view.MyMakerView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/5/20 0020.
 */

public class BeforeMonthFragment extends Fragment {

    private LineChart lc_content;
    private MyMakerView markerView;
    private Drawable drawable;
    private ArrayList<String> arrayX;
    private ArrayList<String> arrayY;

    private JSONArray jsonReviews;

    public BeforeMonthFragment() {
        super();
        arrayY = new ArrayList<>();
        arrayX = new ArrayList<>();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_last_month, container, false);
        lc_content = (LineChart) view.findViewById(R.id.lc_content);
        markerView = new MyMakerView(getContext(), R.layout.chat_maker);
        drawable = ContextCompat.getDrawable(getContext(), R.drawable.rectangle_shadow);
        notifyDataSetChanged();
        return view;
    }

    public void setJsonReviews(JSONArray jsonReviews) {
        this.jsonReviews = jsonReviews;
    }

    public void notifyDataSetChanged() {
        arrayY.clear();
        arrayX.clear();
        try {
            if (jsonReviews != null) {
                for (int i = 0; i < jsonReviews.length(); i++) {
                    JSONObject jsonReview = jsonReviews.getJSONObject(i);
                    arrayY.add(jsonReview.getInt("count") + "");
                    arrayX.add(jsonReview.getString("itemName"));
                }
            }
            LineChartUtil.showChart(lc_content, arrayX, arrayY, markerView, drawable);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
